//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of your Microsoft Windows CE
// Source Alliance Program license form.  If you did not accept the terms of
// such a license, you are not authorized to use this source code.
//
//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2003 Intrinsyc Europe Ltd.
//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2004 Siemens AG
//
// Version: 1.1.2.5
// Date:    11. Jan. 2006
//////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <types.h>
#include <memory.h>
#include <linklist.h>
#include <windev.h>
#include <pkfuncs.h>
#include <pegdser.h>
#include <devload.h>
#include <dbgapi.h>
#include "audprfapi.h"
#include "audprof.h"

#ifdef DEBUG_FILE
int DbgWrite(LPWSTR lpszFmt, ...);
CRITICAL_SECTION	csDbgCritSect;
#endif

#ifdef DEBUG
DBGPARAM dpCurSettings = {
    TEXT("AudProf"), {
        TEXT("Init"),TEXT("Open"),TEXT("Read"),TEXT("Write"),
        TEXT("Close"),TEXT("Ioctl"),TEXT("Thread"),TEXT("Events"),
        TEXT("CritSec"),TEXT("FlowCtrl"),TEXT("Info"),TEXT("User Read"),
        TEXT("Alloc"),TEXT("Function"),TEXT("Warning"),TEXT("Error")},
//	0xFFFF
    0xC013
};
#endif

/*
 @doc INTERNAL
 @func	BOOL | AudProfDllEntry | Process attach/detach api.
 *
 @rdesc The return is a BOOL, representing success (TRUE) or failure (FALSE).
 */
BOOL WINAPI
AudProfDllEntry(HINSTANCE DllInstance, INT Reason, LPVOID Reserved)
{
    switch(Reason) {
        case DLL_PROCESS_ATTACH:
#ifdef DEBUG
#ifndef DEBUG_FILE
	        DEBUGREGISTER(DllInstance);
#endif
//			DEBUGMSG (ZONE_INIT,(TEXT("AudProf: DLL_PROCESS_ATTACH\r\n")));
#endif
            break;

        case DLL_PROCESS_DETACH:
#ifdef DEBUG
//			DEBUGMSG (ZONE_INIT,(TEXT("AudProf: DLL_PROCESS_DETACH\r\n")));
#endif
            break;
    }
    return TRUE;
}

//
//
//
BOOL AudProf_GetProfilesData(const char **pProfilesData)
{
	DEBUGMSG (ZONE_FUNCTION,(TEXT("+AudProf_GetProfilesData()\r\n")));

	*pProfilesData = &szProfilesData[0][0];

	DEBUGMSG (ZONE_FUNCTION,(TEXT("-AudProf_GetProfilesData()\r\n")));
	return TRUE;
}

//
//
//
BOOL AudProf_GetProfilesLines(DWORD *pProfilesLines)
{
	DEBUGMSG (ZONE_FUNCTION,(TEXT("+AudProf_GetProfilesLines()\r\n")));

	*pProfilesLines = dwProfilesLines;

	DEBUGMSG (ZONE_FUNCTION,(TEXT("-AudProf_GetProfilesLines()\r\n")));
	return TRUE;
}

//
//
//
BOOL AudProf_GetProfilesLastLineLength(DWORD *pProfilesLastLineLength)
{
	DEBUGMSG (ZONE_FUNCTION,(TEXT("+AudProf_GetProfilesLastLineLength()\r\n")));

	*pProfilesLastLineLength = dwProfilesLastLineLength;

	DEBUGMSG (ZONE_FUNCTION,(TEXT("-AudProf_GetProfilesLastLineLength()\r\n")));
	return TRUE;
}

//
//
//
BOOL AudProf_GetProfilesVersion(DWORD *pdwVersion)
{
	DWORD	dwHandle, dwSize, i;
	PDWORD	pvsVersionInfo;
	VS_FIXEDFILEINFO	*pvsFixedFileInfo;
	DWORD	dwMSVer, dwLSVer;

	DEBUGMSG (ZONE_FUNCTION,(TEXT("+AudProf_GetProfilesVersion()\r\n")));

	// Fetch the size of the version structure
	dwSize = GetFileVersionInfoSize(TEXT("AUDPROF.DLL"), &dwHandle);

	// Allocate some memory for it
	pvsVersionInfo = (PDWORD)LocalAlloc(LPTR, dwSize);

	// Fetch the version structure
	GetFileVersionInfo(TEXT("AUDPROF.DLL"), dwHandle, dwSize, pvsVersionInfo);

	// Search for the signature
	for(i=0;i<dwSize/4;i++)
	{
		if(pvsVersionInfo[i] == VS_FFI_SIGNATURE)pvsFixedFileInfo = (VS_FIXEDFILEINFO*)&pvsVersionInfo[i];
	}

	DEBUGMSG(ZONE_FUNCTION, (TEXT("Version LS = 0x%X  MS = 0x%X\r\n"), pvsFixedFileInfo->dwFileVersionLS, pvsFixedFileInfo->dwFileVersionMS));

	// Read the version info
	dwMSVer = pvsFixedFileInfo->dwFileVersionMS;
	dwLSVer = pvsFixedFileInfo->dwFileVersionLS;

	// Construct a 32 bit version number
	*pdwVersion = ((dwMSVer&0x00FF0000)<<8) + ((dwMSVer&0x000000FF)<<16) + ((dwLSVer&0x00FF0000)>>8) + (dwLSVer&0x000000FF);

	// Free the memory
	LocalFree(pvsVersionInfo);

	DEBUGMSG (ZONE_FUNCTION,(TEXT("-AudProf_GetProfilesVersion()\r\n")));
	return TRUE;
}

